<template>
  <div class="mainpage clearfix">
    <a-tabs @tabClick="tabClick">
      <a-tab-pane key="1" tab="基本信息">
        <modmessage ref="modemsg"></modmessage
      ></a-tab-pane>
      <a-tab-pane key="2" tab="项目坐标">
        <coordinates ref="coordinates"></coordinates>
      </a-tab-pane>
      <a-tab-pane key="3" tab="外业核查" v-if="wyhc == true">
        <wych ref="wych" :xmjd="xmJd"></wych>
      </a-tab-pane>
      <a-tab-pane key="4" tab="附件"
        ><attachment ref="attachment" :xmJd="xmJd"></attachment
      ></a-tab-pane>
      <a-tab-pane key="5" tab="永久基本农田占用补划更新包">
        <updatepackage ref="updatepackage"></updatepackage>
      </a-tab-pane>
      <a-button
        slot="tabBarExtraContent"
        class="button"
        type="danger"
        @click="datacheck"
        v-if="SJbtn == true"
      >
        数据检查
      </a-button>
      <a-button
        slot="tabBarExtraContent"
        class="btn1 button"
        @click="savedata"
        v-if="Savebtn == true"
      >
        保存
      </a-button>
      <a-button
        slot="tabBarExtraContent"
        class="btn2 button"
        @click="report"
        v-if="Topbtn == true"
      >
        上报
      </a-button>

      <a-dropdown
        slot="tabBarExtraContent"
        class="btn4 button"
        v-if="XMBGBTN == true"
      >
        <a-button> 项目变更 </a-button>
        <a-menu
          slot="overlay"
          @click="handleMenuClick"
          style="text-align: center"
        >
          <a-menu-item
            key="0"
            style="color: black; font-weight: 500; font-size: 16px"
          >
            项目变更相关操作
          </a-menu-item>
          <a-menu-item key="1"> 续期项目 </a-menu-item>
          <a-menu-item key="2"> 停止生产 </a-menu-item>
          <a-menu-item key="3"> 转为其他农业用途 </a-menu-item>
          <a-menu-item key="4"> 改为扩建项目 </a-menu-item>
        </a-menu>
      </a-dropdown>
      <a-button
        v-if="QRBTN == true"
        slot="tabBarExtraContent"
        class="btn3 button"
        @click="submitBG"
      >
        确认变更
      </a-button>
      <a-button
        v-if="SSJC == true"
        slot="tabBarExtraContent"
        class="btn3 button"
        @click="ssjcBtn()"
      >
        设施建成
      </a-button>
      <a-button
        slot="tabBarExtraContent"
        class="button"
        type="primary"
        @click="black"
      >
        返回
      </a-button>
    </a-tabs>
  </div>
</template>
  
  <script>
import modmessage from "./modules/modmessage.vue";
import coordinates from "./modules/coordinates.vue";
import attachment from "./modules/attachment.vue";
import updatepackage from "./modules/updatepackage.vue";
import wych from "./modules/wych.vue";
import {
  submit,
  ssnydLandLsadd,
  dataCheck,
  ssnydlandetial,
  xmjdData,
  renewalProjects,
  extensionProject,
  confirmProject,
} from "../../api/api";
export default {
  name: "home",
  components: { modmessage, coordinates, attachment, updatepackage, wych },
  data() {
    return {
      role: JSON.parse(localStorage.getItem("user")).role,
      xmtype: "",
      SJbtn: true, //控制数据检查按钮
      Savebtn: true, //控制保存按钮
      Topbtn: true, //控制上报按钮
      XMBGBTN: false, //控制项目变更按钮
      QRBTN: false, //控制确认变更按钮
      SSJC: false, //控制设施建成按钮
      wyhc: false, //外业核查
      xmJd: "",
      isbg: "",
      cardid: "",
    };
  },
  created() {},
  mounted() {
    this.getdetaildata();
  },
  methods: {
    handleMenuClick(obj) {
      console.log(obj.key, "kk");
      if (obj.key == 2) {
        this.ssjcBtn(2);
      } else if (obj.key == 3) {
        this.renewalproject(3);
        this.isbg = 2;
      } else if (obj.key == 1) {
        this.renewalproject(1);
        this.isbg = 1;
      } else if (obj.key == 4) {
        this.expansion();
      }
    },
    //确认变更
    async submitBG() {
      if (this.isbg == "") {
        this.$warning({
          title: "数据检查",
          content: "请进行变更操作后确认",
          okText: "确定",
        });
      } else {
        const obj = this.$refs.modemsg;
        const brr = obj.boxcheckedlist.concat(
          obj.cllist,
          obj.qllist,
          obj.sclist
        );
        const btt = Array.from(new Set(brr));
        const parmas = {
          dataSec: this.$route.query.id,
          isbg: this.isbg,
          xmSxqxend: obj.enddate, //结束时间
          xmFzssydmjmx:
            obj.showcheck == false
              ? obj.zwlist.join(",")
              : obj.xqlist.join(","),
          xmXqxl: btt.join(","),
          xmQtfzlxname: obj.xmfzlxname,
          xmSsnylx:
            obj.showcheck == false
              ? obj.zwlist.join(",")
              : obj.xqlist.join(","),
          xmXqdl: obj.xmxqdls,
        };
        const res = await confirmProject(parmas);
        if (res.code == 500) {
          this.$info({
            title: "数据检查",
            content: res.message,
            okText: "确定",
          });
        } else {
          this.$success({
            title: "操作成功",
            okText: "确定",
            onOk: () => {
              return new Promise(async (resolve, reject) => {
                this.getdetaildata();
                this.$router.go(-1);
                resolve();
              }).catch();
            },
          });
        }
      }
    },
    //扩建项目
    async expansion() {
      const params = {
        datasec: this.$route.query.id,
      };
      const res = await extensionProject(params);
      if (res.code == 500) {
        this.$info({
          title: "数据检查",
          content: res.message,
          okText: "确定",
        });
      } else {
        this.$success({
          title: "操作成功",
          okText: "确定",
          onOk: () => {
            return new Promise(async (resolve, reject) => {
              this.$router.go(-1);
              resolve();
            }).catch();
          },
        });
        this.getdetaildata();
      }
    },
    //续期项目和转为其他农业用途接口
    async renewalproject(type) {
      const params = {
        datasec: this.$route.query.id,
        filetype: 0,
        fkflow: "001",
        isbg: type == 3 ? 2 : 1,
        isedit: type == 3 ? 1 : 0,
      };
      const res = await renewalProjects(params);
      if (res.code == 500) {
        this.$info({
          title: "数据检查",
          content: res.message,
          okText: "确定",
        });
      } else {
        this.$success({
          title: "操作成功",
          okText: "确定",
          onOk: () => {
            return new Promise(async (resolve, reject) => {
              if (type == 3) {
                this.$message.warn("请点击确认变更");
              } else {
                this.$router.go(-1);
              }

              this.getdetaildata();
              // this.$refs.attachment.getdata();
              resolve();
            }).catch();
          },
        });
      }
    },
    //根据详情接口获取数据
    async getdetaildata() {
      const obj = this.$refs.modemsg;
      if (this.role == "shi" || this.role == "sheng") {
        this.SJbtn = false;
        this.Savebtn = false;
        this.Topbtn = false;
      }

      if (this.$route.query.id) {
        const params = {
          id: this.$route.query.id,
        };
        this.xmtype = this.$route.query.diff;
        const data = await ssnydlandetial(params);
        const res = data.result;
        console.log(res.xmJd, "11111111");
        const xmjd = res.xmJd; //项目阶段
        this.xmJd = res.xmJd;
        const xmzt = res.xmZt;
        if (xmzt == 1) {
          this.SJbtn = false;
          this.Savebtn = false;
          this.Topbtn = false;
          obj.isipt = true;
        }
        if (
          this.role == "shi" ||
          this.role == "sheng" ||
          xmjd == "9" ||
          xmjd == "3"
        ) {
          this.SJbtn = false;
          this.Savebtn = false;
          this.Topbtn = false;
          this.XMBGBTN = false;
          this.QRBTN = false;
          obj.isipt = true;
          this.SSJC = false;
          if (xmjd == "1" || xmjd == "2") {
            this.wyhc = true;
          }
        } else {
          if (this.$route.query.diff == "xn") {
            //对外业核查得判断
            if (xmjd == "1" || xmjd == "2") {
              this.wyhc = true;
            }
            //对按钮得判断
            if (xmjd == "0" || xmjd == "" || xmjd == null) {
              this.Savebtn = false;
              this.SSJC = true;
              this.Topbtn = false;
              this.SJbtn = false;
              this.XMBGBTN = false;
              this.QRBTN = false;
            } else if (xmjd == "1") {
              this.Savebtn = true;
              this.SJbtn = true;
              this.Topbtn = true;
              this.SSJC = false;
              this.XMBGBTN = false;
              this.QRBTN = false;
              obj.isinput = true;
              obj.isipt = false;
            } else if (xmjd == "2") {
              this.Savebtn = false;
              this.SJbtn = false;
              this.Topbtn = false;
              this.SSJC = false;
              this.XMBGBTN = true;
              this.QRBTN = true;
            } else if (xmjd == "3") {
              this.Savebtn = false;
              this.SJbtn = false;
              this.Topbtn = false;
              this.SSJC = false;
              this.XMBGBTN = false;
              this.QRBTN = false;
            }
          } else if (this.$route.query.diff == "xq") {
            this.SJbtn = false;
            this.Savebtn = false;
            this.Topbtn = false;
            this.XMBGBTN = false;
            this.QRBTN = false;
            this.SSJC = false;
          }
        }
      }
    },
    //点击tab时回调
    tabClick(e) {
      this.cardid = e;
      if (e == 4) {
        this.$refs.attachment.getdata();
      } else if (e == 5) {
        this.$refs.updatepackage.getdatalist();
      } else if (e == 3) {
        this.$refs.wych.getdata();
      }

      // this.$refs.athment.id = this.id
    },
    //数据检查
    async datacheck() {
      let obj = this.$refs.modemsg;
      const params = {
        id: obj.id == undefined ? "" : obj.id,
      };
      const data = await dataCheck(params);
      if (data.code == 500) {
        this.$info({
          title: "数据检查",
          width: 500,
          content: (h) => {
            return h("div", { domProps: { innerHTML: data.message } });
          },

          okText: "确定",
        });
      } else {
        this.$warning({
          title: "数据检查",
          content: "数据检查通过",
          okText: "确定",
        });
      }
    },
    save() {},
    //保存
    async savedata() {
      var obj = this.$refs.modemsg;
      if (
        this.$route.query.type == "detail" ||
        this.$route.query.type == "add"
      ) {
        const sumnum = Number(obj.scmj) + Number(obj.fzscyd);
        const oddnum = obj.nysqmj == "" ? 0 : Number(obj.nysqmj);

        if (obj.dw == "" || obj.dw == null) {
          this.$info({
            title: "数据检查",
            content: "请选择生产规模的单位!",
            okText: "确定",
          });
        } else if (obj.informer == "" || obj.dw == null) {
          this.$info({
            title: "数据检查",
            content: "请输入填报人!",
            okText: "确定",
          });
        } else if (oddnum > sumnum.toFixed(5) && sumnum > 0) {
          this.$confirm({
            content:
              "设施农业申请用地面积不等同于生产用地面积+辅助生产用地面积，是否继续保存？",
            okText: "确定",
            cancelText: "取消",
            okText: "确定",
            onOk: () => {
              return new Promise(async (resolve, reject) => {
                this.saveaddData();
                resolve();
              }).catch();
            },
            onCancel() {
              console.log("Cancel");
            },
          });
        } else if (oddnum < sumnum.toFixed(5)) {
          this.$info({
            content:
              "设施农业申请用地面积不能小于生产用地面积+辅助生产用地面积",
            okText: "确定",
          });
        } else {
          if (
            this.$refs.modemsg.projectname == "" ||
            this.$refs.modemsg.projectname == undefined
          ) {
            this.$info({
              title: "数据检查",
              content: "项目名称不能为空!",
              okText: "确定",
            });
          } else {
            this.saveaddData();
          }
        }
      } else {
        const sumnum = Number(obj.scmj) + Number(obj.fzscyd);
        const oddnum = obj.nysqmj == "" ? 0 : Number(obj.nysqmj);
        if (oddnum > sumnum.toFixed(5)) {
          this.$confirm({
            content:
              "设施农业申请用地面积不等同于生产用地面积+辅助生产用地面积，是否继续保存？",
            okText: "确定",
            cancelText: "取消",
            onOk: () => {
              return new Promise(async (resolve, reject) => {
                this.saveaddData();
                resolve();
              }).catch();
            },
            onCancel() {
              console.log("Cancel");
            },
          });
        } else if (oddnum < sumnum.toFixed(5)) {
          this.$info({
            content:
              "设施农业申请用地面积不能小于生产用地面积+辅助生产用地面积",
            okText: "确定",
          });
        } else {
          if (
            this.$refs.modemsg.projectname == "" ||
            this.$refs.modemsg.projectname == undefined
          ) {
            this.$info({
              title: "数据检查",
              content: "项目名称不能为空!",
              okText: "确定",
            });
          } else {
            this.saveaddData();
          }
        }
      }
    },
    async saveaddData() {
      const obj = this.$refs.modemsg;
      if (Number(obj.sumtz) > 10000) {
        this.$confirm({
          content: "项目总投资金额为" + obj.sumtz + " 万元，是否继续保存？",
          okText: "确定",
          cancelText: "取消",
          onOk: () => {
            return new Promise(async (resolve, reject) => {
              this.SumSavedata();
              resolve();
            }).catch();
          },
          onCancel() {
            console.log("Cancel");
          },
        });
      } else if (Number(obj.nysqmj) > 100) {
        this.$confirm({
          content:
            "设施农业申请用地面积为" + obj.nysqmj + " 公顷，是否继续保存？",
          okText: "确定",
          cancelText: "取消",
          onOk: () => {
            return new Promise(async (resolve, reject) => {
              this.SumSavedata();
              resolve();
            }).catch();
          },
          onCancel() {
            console.log("Cancel");
          },
        });
      } else {
        this.SumSavedata();
      }
    },
    //数据总保存
    async SumSavedata() {
      const obj = this.$refs.modemsg;
      const brr = obj.boxcheckedlist.concat(obj.cllist, obj.qllist, obj.sclist);
      const btt = Array.from(new Set(brr));
      // console.log(obj.xmjd, "btt");

      // return
      const params = {
        type: obj.id == undefined ? 1 : 2,
        dataSec: obj.id,
        xmFzlbFzxm: "",
        xmName: obj.projectname,
        xmUsepoint: obj.city,
        xmUsepoint1: obj.xian,
        xmUsepoint2: obj.xz,
        xmTdsyqname: obj.cun,
        xmLrrxm: obj.informer,
        xmLrrq: obj.tbdatetime,
        dkSum: obj.sumcount,
        xmTbrxm: obj.sqname,
        xmTbrdh: obj.sqphone,
        xmBabh: obj.banum,
        xmBarq: obj.badate,
        xmSxqxstart: obj.begindate,
        xmSxqxend: obj.enddate,
        xmScgm: Number(obj.scgm),
        xmScgmdw: obj.dw,
        xmZtz: obj.sumtz,
        xmJzjgjcc: obj.jzjg,
        xmJgcc: obj.xm_jgcc,
        sslx1: obj.ischek == true ? "1" : "",
        sslxXl1: obj.childrencheck == true ? "ZW" : "",
        sslx2: obj.ischek2 == true ? "2" : "",
        sslxXl2: obj.cldata == true ? "CL" : "",
        sslxXl3: obj.qldata == true ? "QL" : "",
        sslxXl4: obj.scdata == true ? "SC" : "",
        xmNyydmj: obj.nyscmj,
        xmZydmj: obj.nysqmj,
        xmScssydmj: obj.scmj,
        xmYdbhqksm: obj.xmydbhqksm,
        jtxm: btt.join(","),
        xmFzssydmj: obj.fzscyd,
        xmTdxz:
          obj.stateowned == true && obj.collective == true
            ? "GY,JT"
            : obj.stateowned == true
            ? "GY"
            : obj.collective == true
            ? "JT"
            : "",
        xmSyndtmj: obj.qznyyd,
        xmSygdmj: obj.sygd,
        xmPhgdgzcmj: obj.phgdzc,
        xmQzjbntmj: obj.syjbnt,
        xmQzphyjjbntgzcmj: obj.phjbnt,
        xmBhyjjbntmj: obj.jhyjnt,
        xmSsnylxmx:
          obj.showcheck == false ? obj.zwlist.join(",") : obj.xqlist.join(","),
        xmLrrdw: obj.xmLrrdw,
        xmQtfzlxname: obj.xmfzlxname,
        xmZt: obj.xmzt,
        xmJd: this.xmJd,
      };
      const jsdata = JSON.stringify(params);
      const data = await ssnydLandLsadd(jsdata);
      if (data.code == 200) {
        this.$success({
          title: "数据检查",
          content: "保存成功!",
          okText: "确定",
          onOk: () => {
            return new Promise(async (resolve, reject) => {
              if (
                this.$route.query.type == "detail" ||
                this.$route.query.type == "add"
              ) {
                if (this.$route.query.diff == "xn") {
                  this.getdetaildata();
                } else {
                  this.$refs.modemsg.getdetail();
                }
              } else {
                this.Cleardata();
              }
              resolve();
            }).catch();
          },
        });

        this.$store.dispatch("fetchData");
      } else {
        this.$info({
          title: "数据检查",
          content: data.message,
          okText: "确定",
        });
      }
    },

    //上报
    async report() {
      if (this.$route.query.id != undefined) {
        const obj = this.$refs.modemsg;
        if (this.$route.query.diff == "xn") {
          const params = {
            datasec: this.$route.query.id,
            xmjd: "2",
            ischeck: "ischeck",
          };
          const data = await xmjdData(params);
          if (data.code == 500) {
            this.$warning({
              title: "数据检查",
              width: 500,
              content: (h) => {
                return h("div", { domProps: { innerHTML: data.message } });
              },
              okText: "确定",
            });
          } else {
            this.$success({
              title: "数据检查",
              width: 500,
              content: (h) => {
                return h("div", { domProps: { innerHTML: "上报成功" } });
              },
              okText: "确定",
              onOk: () => {
                return new Promise(async (resolve, reject) => {
                  if (
                    this.$route.query.type == "detail" ||
                    this.$route.query.type == "add"
                  ) {
                    this.getdetaildata();
                    this.$refs.modemsg.getdetail();
                    if (this.cardid == 3) {
                      this.$refs.wych.getdata();
                    }
                    resolve();
                  } else {
                    this.Cleardata();
                    resolve();
                  }
                }).catch();
              },
            });
          }
        } else {
          const params = {
            ids: obj.id == undefined ? "" : obj.id,
            status: "1",
          };
          const data = await submit(params);
          if (data.code == 500) {
            this.$warning({
              title: "数据检查",
              width: 500,
              content: (h) => {
                return h("div", { domProps: { innerHTML: data.message } });
              },
              okText: "确定",
            });
          } else {
            this.$success({
              title: "数据检查",
              width: 500,
              content: (h) => {
                return h("div", { domProps: { innerHTML: data.message } });
              },
              okText: "确定",
              onOk: () => {
                return new Promise(async (resolve, reject) => {
                  if (
                    this.$route.query.type == "detail" ||
                    this.$route.query.type == "add"
                  ) {
                    this.getdetaildata();
                  } else {
                    this.Cleardata();
                  }
                  resolve();
                }).catch();
              },
            });
          }
        }
      } else {
        this.$warning({
          title: "数据检查",
          width: 500,
          content: (h) => {
            return h("div", { domProps: { innerHTML: "暂无需要上报得数据" } });
          },
          okText: "确定",
        });
      }
    },
    //返回
    black() {
      if (
        this.$route.query.type == "detail" ||
        this.$route.query.type == "add"
      ) {
        this.$router.go(-1);
      }
    },
    //设施建成
    async ssjcBtn(type) {
      const params = {
        datasec: this.$route.query.id,
        xmjd: 1,
        ischeck: "ischeck",
      };
      const res = await xmjdData(params);
      if (res.code == 500) {
        this.$info({
          title: "数据检查",
          content: (h) => {
            return h("div", { domProps: { innerHTML: res.message } });
          },
          okText: "确定",
        });
      } else {
        this.$success({
          title: "设施建成成功",
          okText: "确定",
          onOk: () => {
            return new Promise(async (resolve, reject) => {
              this.getdetaildata();
              resolve();
            }).catch();
          },
        });
      }
    },
    //项目变更
    popover() {},
    //清空数据
    Cleardata() {
      const obj = this.$refs.modemsg;
      obj.xz = "";
      obj.ischek = false;
      obj.childrencheck = false;
      obj.ischek2 = false;
      obj.isinput = false;
      obj.isipt = false;
      obj.iptype = "";
      obj.showcheck = false;
      obj.isboxchecked = false;
      obj.boxcheckedlist = [];
      obj.cldata = false;
      obj.cllist = [];
      obj.qldata = false;
      obj.scdata = false;
      obj.qllist = [];
      obj.scdat = [];
      obj.sclist = [];
      obj.stateowned = true;
      obj.collective = true;
      obj.projectname = "";
      obj.informer = "";
      obj.zwlist = [];
      obj.xqlist = [];
      obj.xz = "";
      obj.cun = "";
      obj.sumcount = "";
      obj.sqname = "";
      obj.sqphone = "";
      obj.banum = "";
      obj.badate = "";
      obj.begindate = "";
      obj.enddate = "";
      obj.scgm = "";
      obj.dw = "";
      obj.sumtz = "";
      obj.jzjg = "";
      obj.xm_jgcc = "1";
      obj.nyscmj = "";
      obj.nysqmj = "";
      obj.scmj = "";
      obj.fzscyd = "";
      obj.qznyyd = "";
      obj.sygd = "";
      obj.phgdzc = "";
      obj.syjbnt = "";
      obj.phjbnt = "";
      obj.jhyjnt = "";
      obj.tbdatetime = "";
      obj.xmLrrdw = "";
      obj.xmfzlxname = "";
    },
  },
};
</script>
  
  <style scoped lang="less">
.btn1 {
  background-color: green;
  color: white;
}
.btn2 {
  background-color: #447894;
  color: white;
}
.btn3 {
  background-color: #16d17f;
  color: white;
}
.btn4 {
  background-color: #33cabb;
  color: white;
}
.button {
  margin-left: 20px;
}
</style>
  